#!/bin/bash +x

#function usage
usage()
{
  echo "usage: mkc [class_name] [project_name]"
  exit 1
}

check_project(){
  if [[ ! -d "${PROJECT_HOME}" || 
        ! -d "${PROJECT_HOME}/include"  || 
        ! -d "${PROJECT_HOME}/tests"  ||
        ! -d "${PROJECT_HOME}/src"  ]]; then
    echo "Class creating failed:$PROJECT_HOME is not a valid project structure."
    exit 1		
  fi

  if [[  -d "${PROJECT_HOME}/tests/${CLASS_NAME}" ||
        -e "${PROJECT_HOME}/include/${CLASS_NAME}.h" ||
        -e "${PROJECT_HOME}/src/${CLASS_NAME}.cc"  ]]; then
    echo "Class creating failed:${CLASS_NAME} exists."
    exit 1	
  fi	
}

TEMPLATE_DIR=${OOBC_HOME}/shared/template

if [ -n "$1" ]; then
  CLASS_NAME=$1
else
  echo "Class creating failed:class name missing."
  usage
fi
	
if [ -n "$2" ]; then
  PROJECT_HOME=${WORKSPACE_DIR}/$2
elif [ -n "$PHOME" ]; then
  PROJECT_HOME=${PHOME}
else
  PROJECT_HOME=`pwd`	
fi

check_project

echo "Project is under $PROJECT_HOME."	
		
mkdir ${PROJECT_HOME}/tests/${CLASS_NAME}

cp ${TEMPLATE_DIR}/TestMakeFileTemplate ${PROJECT_HOME}/tests/${CLASS_NAME}/Makefile

sed "s/Foo/${CLASS_NAME}/g" ${TEMPLATE_DIR}/TestTemplate.h > ${PROJECT_HOME}/tests/${CLASS_NAME}/Test${CLASS_NAME}.h

declare -u UPPER_CLASS_NAME=$CLASS_NAME #UPPER_CLASS_NAME=`echo $CLASS_NAME|tr [:lower:] [:upper:]`
sed "s/Foo/${CLASS_NAME}/g;s/FOO/${UPPER_CLASS_NAME}/g" ${TEMPLATE_DIR}/HeaderFileTemplate.h > ${PROJECT_HOME}/include/${CLASS_NAME}.h

sed "s/Foo/${CLASS_NAME}/g" ${TEMPLATE_DIR}/SourceFileTemplate.cc > ${PROJECT_HOME}/src/${CLASS_NAME}.cc

echo "Class ${CLASS_NAME} created under $PROJECT_HOME."
